package com.ericsson.enap.modules.procLog.utils;

import com.ericsson.common.util.LogFileName;
import com.ericsson.common.util.LoggerUtils;
import com.ericsson.common.util.UncompressFileGZIPUtil;
import com.ice.tar.TarInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.slf4j.Logger;

import java.io.*;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
 * @description: ProcLogUtil工具类
 * @author: wangjingshi
 * @create: 2021-05-06 16:15
 */
public class ProcLogUtil {

    private static Logger log = LoggerUtils.Logger(LogFileName.ProcLogJob);

    /**
     * 解压tar文件
     * @param destPath
     * @param tais
     * @throws Exception
     */
    public static void deTarFile(String destPath, TarArchiveInputStream tais) throws Exception {
        TarArchiveEntry tae = null;
        while ((tae = tais.getNextTarEntry()) != null) {
            System.out.println("-------------"+tae.getName());
            String dir = destPath + tae.getName().split("");//tar档中文件
            System.out.println("-------------"+dir);
            File dirFile = new File(dir);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dirFile));

            int count;
            byte data[] = new byte[1024];
            while ((count = tais.read(data, 0, 1024)) != -1) {
                bos.write(data, 0, count);
            }
            bos.close();
        }
    }

    /**
     * 解压缩下载的文件
     */
    public static void uncompress(File file, String path) {
        InputStream inputstream = null;
        OutputStream outputstream = null;
        TarInputStream zis = null;
        GZIPInputStream gzis = null;
        try {
            if (file.getName().endsWith(".tar")) {
                File tempFile = null;
                inputstream = new FileInputStream(file);
                zis = new TarInputStream(inputstream);
                while (zis.getNextEntry() != null) {
                    tempFile = new File(path);
                    tempFile.createNewFile();
                    outputstream = new FileOutputStream(tempFile);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = zis.read(buffer)) > 0) {
                        outputstream.write(buffer, 0, len);
                    }
                }
            } else if (file.getName().endsWith(".gz")) {
                log.info("==========================  uncompress name:"+file.getName());
                UncompressFileGZIPUtil.doProcUncompressFile(path, file, file.getName());
            }
        } catch (FileNotFoundException e) {
            log.info(">> ProcLogUtil uncompress failre.Cause by" + e.getMessage());
        } catch (IOException e) {
            log.info(">> ProcLogUtil uncompress failre.Cause by" + e.getMessage());
        } finally {
            try {
                if (outputstream != null) {
                    outputstream.flush();
                    outputstream.close();
                }
                if (inputstream != null) {
                    inputstream.close();
                }
                if (zis != null) {
                    zis.close();
                }
                if (gzis != null) {
                    gzis.close();
                }
            } catch (Exception e) {
                log.info(">> ProcLogUtil uncompress failre.Cause by" + e.getMessage());
            }
        }
    }

}
